package com.hyd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hyd.common.annotation.Log;
import com.hyd.common.core.controller.BaseController;
import com.hyd.common.core.domain.AjaxResult;
import com.hyd.common.enums.BusinessType;
import com.hyd.manage.domain.Medicines;
import com.hyd.manage.service.IMedicinesService;
import com.hyd.common.utils.poi.ExcelUtil;
import com.hyd.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 药品入库Controller
 *
 * @author hwb
 * @date 2025-02-20
 */
@Api(tags = "药品入库Controller")
@RestController
@RequestMapping("/manage/inventory")
public class InventoryController extends BaseController
{
    @Autowired
    private IMedicinesService medicinesService;

    @ApiOperation("查询库存详细")
    @PreAuthorize("@ss.hasPermi('manage:medicines:list')")
    @Log(title = "药品库存", businessType = BusinessType.OTHER)
    @GetMapping("/{medicineCode}")
    public TableDataInfo getInventory(@PathVariable("medicineCode") String medicineCode)
    {
        startPage();
        List<Medicines> list = medicinesService.selectInventoryByCode(medicineCode);
        // 为r_stockQuantity字段赋值
        for (Medicines medicine : list) {
            medicine.setR_stockQuantity(medicine.getStockQuantity());
        }
        return getDataTable(list);
    }

    /**
     * 修改药品库存
     */
    @ApiOperation("修改药品库存")
    @PreAuthorize("@ss.hasPermi('manage:medicines:edit')")
    @Log(title = "药品库存", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody List<Medicines> medicines)
    {
        for (Medicines medicine : medicines) {
            medicinesService.updateMedicines(medicine);
        }
        return AjaxResult.success();
    }
}